/**
 * 
 */
package ar.edu.unq.svnpermission.panels;

import java.util.Iterator;
import java.util.List;

import javax.swing.tree.TreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import ar.edu.unq.svnpermission.components.AutoAttacheablePanel;
import ar.edu.unq.svnpermission.domain.Folder;
import ar.edu.unq.svnpermission.ui.model.Folders;
import ar.edu.unq.svnpermission.ui.model.NewFolder;
import ar.edu.unq.svnpermission.ui.pages.NewFolderPage;

import org.apache.wicket.Component;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.tree.BaseTree;
import org.apache.wicket.markup.html.tree.LinkIconPanel;
import org.apache.wicket.markup.html.tree.LinkTree;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.ajax.AjaxRequestTarget;

/**
 * @author nicolas
 *
 */
public class TreePanel extends AutoAttacheablePanel {

	private static final long serialVersionUID = 1L;	
	private BaseTree tree;
	
	public TreePanel(String id, MarkupContainer container) {
		super(id, container);
	}

	@Override
	public void build() {

        this.setTree(this.createTree());
        this.add(this.getTree());
        this.addLinks();
	}

	public LinkTree createTree()
	{
		final Folders folderModel = (Folders)this.getParent().getDefaultModelObject();
		LinkTree linkTree = new LinkTree("tree",this.convertToTreeModel()){
			private static final long serialVersionUID = 1L;

			@Override
	        protected Component newNodeComponent(String id, IModel<Object> model) {
	          final Folder item = (Folder) ((DefaultMutableTreeNode) model.getObject()).getUserObject();
			  if (item!= null) {
				final LinkIconPanel panel = new LinkIconPanel(id, model, this) {
					private static final long serialVersionUID = 1L;
				
				  	protected void onNodeLinkClicked(Object node, BaseTree tree, AjaxRequestTarget target)
					{
						super.onNodeLinkClicked(node, tree, target);
						folderModel.setSelectedFolder(item);
						this.getPage().get("treeForm").modelChanged();
						FolderDetailPanel folderDetailPanel = (FolderDetailPanel)this.getPage().get("treeForm").get("folderDetail");				
						this.getPage().get("treeForm").get("folderDetail").modelChanged();
						this.getPage().get("treeForm").get("folderDetail").setVisible(true);
						target.add(folderDetailPanel);						
						TreePanel.this.get("newFolderLink").get("selectedFolder").setDefaultModelObject(folderModel.getSelectedFolder());
						TreePanel.this.get("newFolderLink").get("selectedFolder").setOutputMarkupId(true);
						target.add(TreePanel.this.get("newFolderLink"));
						target.add(TreePanel.this.get("newFolderLink").get("selectedFolder"));
					}	              
				  
				};
				
				return panel;
			
			  } else {
			    // Standard node creation
			    return super.newNodeComponent(id, model);
			  }
	        }
		
		};
		return linkTree;
	}

	public BaseTree getTree() {
		return tree;
	}

	public void setTree(BaseTree tree) {
		this.tree = tree;
	}
	
    private TreeModel convertToTreeModel()
    {
        TreeModel model = null;
        Folder rootFolder = new Folders().getRootFolder();
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(rootFolder);
        this.add(rootNode, rootFolder.getChildFolders());
        model = new DefaultTreeModel(rootNode);
        return model;
    }

    private void add(DefaultMutableTreeNode parent, List<Folder> sub)
    {
        for (Iterator<Folder> i = sub.iterator(); i.hasNext();)
        {
            Folder folder = i.next();
            if(folder!=null)
            {
	            if (folder.getChildFolders()==null || folder.getChildFolders().isEmpty())
	            {
	                DefaultMutableTreeNode child = new DefaultMutableTreeNode(folder);
	                parent.add(child);	                
	            }
	            else
	            {
	                DefaultMutableTreeNode child = new DefaultMutableTreeNode(folder);
	                parent.add(child);
	                add(child, folder.getChildFolders());
	            }
            }
        }
    }	

    private void addLinks()
    {
    	this.addNewFolderLink();
    }
    
    private void addNewFolderLink()
    {
    	@SuppressWarnings("rawtypes")
		Link newFolderLink = new Link("newFolderLink"){
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				setResponsePage(new NewFolderPage(((Folders)TreePanel.this.getParent().getDefaultModelObject()).getSelectedFolder()));
			}
    		
    	};
    	Label folderLabel = new Label("selectedFolder","No hay carpetas seleccionadas");
    	folderLabel.setOutputMarkupId(true);
    	newFolderLink.add(folderLabel);
    	newFolderLink.setOutputMarkupId(true);
    	newFolderLink.setVisible(true);
    	this.add(newFolderLink);  	
    }
    
}
